package org.railage.server.services;

import java.util.List;

import org.railage.common.dto.TripDTO;
import org.railage.common.dto.TripThroughTwoStationsDTO;
import org.railage.common.protocol.Result;
import org.railage.server.dao.TripDAO;
import org.railage.server.dao.TripDAOImpl;
import org.railage.server.dao.TripThroughTwoStationsDAO;
import org.railage.server.dao.TripThroughTwoStationsDAOImpl;
import org.railage.server.entities.Trip;
import org.railage.server.entities.TripThroughTwoStations;
import org.railage.server.util.HibernateUtil;

public class TripService {
	
	private TripDAO tripDAO = new TripDAOImpl();
	private TripThroughTwoStationsDAO tripThroughTwoStationsDAO = new TripThroughTwoStationsDAOImpl();

	public Result addTrip(Object obj) {
		Object[] params = (Object[]) obj;
		String routeName = (String) params[0];
		int seatsNumber = (Integer) params[1];
		String startTimestamp = (String) params[2];
		
		Result result = null;
		int resultCode = tripDAO.addTrip(routeName, seatsNumber, startTimestamp);
		switch (resultCode) {
		case 0:
			result = new Result(true, "Trip added");
			break;
		case 1:
			result = new Result(false, "Route " + routeName + " does not exist!");
			break;
		case -1:
			result = new Result(false, HibernateUtil.DB_SESSION_ERROR_MSG);
			break;
		default:
			break;
		}
		return result;
	}

	public Result deleteTrip(Object obj) {
		int tripID = (Integer) obj;
		Result result = null;
		int resultCode = tripDAO.deleteTrip(tripID);
		switch (resultCode) {
		case 0:
			result = new Result(true, "Trip #" + tripID + " deleted");
			break;
		case 1:
			result = new Result(false, "Some tickets on trip #" + tripID + " are sold and train did not finish!");
			break;
		case -1:
			result = new Result(false, HibernateUtil.DB_SESSION_ERROR_MSG);
			break;
		default:
			break;
		}
		return result;
	}

	public String[] getTripIDsByRouteName(Object obj) {
		String routeName = (String) obj;
		List<Trip> trips = tripDAO.getTripsByRouteName(routeName);
		String[] tripIDs = new String[trips.size()];
		int i = 0;
		for (Trip trip : trips) {
			tripIDs[i] = String.valueOf(trip.getId());
			i++;
		}
		return tripIDs;
	}

	public TripDTO[] getAllTrips(Object obj) {
		List<Trip> trips = tripDAO.getAllTrips();
		TripDTO[] tripsDTO = new TripDTO[trips.size()];
		int i = 0;
		for (Trip trip : trips) {
			tripsDTO[i] = trip.toDTO();
			i++;
		}
		return tripsDTO;
	}

	public TripThroughTwoStationsDTO[] getTripsThroughTwoStations(Object obj) {
		String[] params = (String[]) obj;
		List<TripThroughTwoStations> trips = null;
		if (params.length == 2) {
			trips = tripThroughTwoStationsDAO.getTripsThroughTwoStations(
					params[0], params[1]);
		} else if (params.length == 3) {
			trips = tripThroughTwoStationsDAO.getTripsThroughTwoStations(
					params[0], params[1], params[2]);
		} else if (params.length == 4) {
			trips = tripThroughTwoStationsDAO.getTripsThroughTwoStations(
					params[0], params[1], params[2], params[3]);
		}
		TripThroughTwoStationsDTO[] tripsDTO = new TripThroughTwoStationsDTO[trips
				.size()];
		int i = 0;
		for (TripThroughTwoStations trip : trips) {
			tripsDTO[i] = trip.toDTO();
			i++;
		}
		return tripsDTO;
	}
	
}
